package com.digital.smartgrow.service.businesscenter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.dao.businesscenter.NewsMapper;
import com.digital.smartgrow.dto.businesscenter.NewsPageDTO;
import com.digital.smartgrow.entity.businesscenter.News;
import com.digital.smartgrow.service.businesscenter.NewsService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 资讯管理 服务实现类
 * </p>
 *
 * @author xiewb
 * @since 2022-04-16
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Override
    public Object getListByPage(NewsPageDTO pageDTO) {
        Page<News> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq(pageDTO.getStatus() != null, "status", pageDTO.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(pageDTO.getCategory()), "category", pageDTO.getCategory());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getTitle()), "title", pageDTO.getTitle());
        queryWrapper.ge(pageDTO.getStartDate() != null, "updateTime", pageDTO.getStartDate());
        queryWrapper.le(pageDTO.getEndDate() != null, "updateTime", pageDTO.getEndDate());
        queryWrapper.orderByDesc("ranking", "updateTime");
        return this.page(page, queryWrapper);
    }

    @Override
    public Object getInfo(Integer id) {
        return this.getById(id);
    }
}
